<?php 
/*
  Plugin Name: JsonReplaceWord
  Plugin URI: http://git.oschina.net/ymyg/json-replaceword
  Version: 0.1
  Author: jsongkong
  Author URI: www.italkyou.cn
  Description: 词语替换插件： 1、替换指定词语 2、删除指定词语
*/
if(!class_exists("JsonReplaceWord")){
    class JsonReplaceWord {
        var $adminOptionsName = "jsonReplaceWordOptions";

        function __construct(){}

        function init(){
            $this->getAdminOptions();
        }

        function getAdminOptions(){
            $jsonReplaceWordOptions = array(
                'json_keyword' => '替换词语',
                'json_replace' => '替换后词语'
            );
            $jsonOptions = get_option($this->adminOptionsName);
            if(!empty($jsonOptions)){
                foreach($jsonOptions as $key => $option){
                    $jsonReplaceWordOptions[$key] = $option;
                }
            }
            update_option($this->adminOptionsName,$jsonReplaceWordOptions);
            return $jsonReplaceWordOptions;
        }

        function replaceWordMainpage(){
            $jsonOptions = $this->getAdminOptions();
            if(isset($_POST['update_message'])){
                if(isset($_POST['json_keyword'])){
                    $jsonOptions['json_keyword'] = $_POST['json_keyword'];
                }
                if(isset($_POST['json_replace'])){
                    $jsonOptions['json_replace'] = $_POST['json_replace'];
                }
                update_option($this->adminOptionsName,$jsonOptions);
            }
            ?>
            <form name="formamt" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
                <div class="json-wrap">
                    JsonCopyright is active. <br/><br/>
                    <h2>JsonReplaceWord Config（版权插件配置）</h2>
                    <div id="json-msgall">
                        <fieldset class="json-options">
                            <legend>keyword（原字符串）:</legend>
                            <p>
                                <textarea style="width:100%;" rows="5" cols="40" name="json_keyword"><?php  _e(apply_filters('format_to_edit',$jsonOptions['json_keyword']), 'JsonReplaceWord') ?></textarea>
                            </p>
                        </fieldset>
                        <fieldset class="json-options">
                            <legend>replacement（替代字符串）:</legend>
                            <p>
                                <textarea style="width:100%;" rows="5" cols="40" name="json_replace"><?php  _e(apply_filters('format_to_edit',$jsonOptions['json_replace']), 'JsonReplaceWord') ?></textarea>
                            </p>
                        </fieldset>
                    </div>
                    <p class="json-submit">
                        <input type="submit" value="<?php _e('Update Settings', 'JsonReplaceWord') ?>" name="update_message" />
                    </p>
                </div>
            </form>
        <?php
        }

        function addOptionsPage(){
            global $replaceWord;
            if(!isset($replaceWord)){
                return ;
            }
            /*加到 “设置”主菜单下*/
            if(function_exists('add_options_page')){
                add_options_page("jsonReplaceWord","Repalce Word",8,basename(__FILE__),array(&$this,'replaceWordMainpage'));
            }
        }

        function addHeaderCode(){
            wp_register_style( 'json-replaceword-css-css', plugins_url( 'css/css.css' , __FILE__ ) );
            wp_enqueue_style( 'json-replaceword-css-css' );
        }

        function doReplaceWord($content){
            $jsonOptions = get_option($this->adminOptionsName);
            $search = $jsonOptions['json_keyword'];
            $replace = $jsonOptions['json_replace'];
            $content = str_replace($search,$replace,$content);
            return $content;
        }
    }
}

if(class_exists("JsonReplaceWord")){
    $replaceWord = new JsonReplaceWord();
}
if(isset($replaceWord)){
    add_action('active_json-replaceword/json-replaceword.php', array(&$replaceWord,'init'));
    add_action('admin_menu', array(&$replaceWord,'addOptionsPage'));
    add_action('admin_enqueue_scripts',array(&$replaceWord,'addHeaderCode'),1); //只在后台加载css
    // add_action('wp_head',array(&$replaceWord,'addHeaderCode'),1); //只在前台加载css
    // add_action('init',array(&$replaceWord,'addHeaderCode'),1); //前后台都加载css
    add_filter('the_content', array(&$replaceWord, 'doReplaceWord'));
}

/*插件页面添加“设置”选项*/
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'plugin_action_links_example' );
function plugin_action_links_example( $links ) {
    $links[] = '<a href="' . get_admin_url( null, 'options-general.php?page=json-replaceword' ) . '">' . __( 'Settings' ) . '</a>';
    return $links;
}